import { Provide, makeHttpRequest } from '@midwayjs/core';
import { WeatherEmptyDataError } from '../error/weather.error';
import { WeatherInfo } from '../interface';

@Provide()
export class WeatherService {


  async getWeather(cityId: string): Promise<WeatherInfo> {
    if (!cityId) {
      throw new WeatherEmptyDataError();
    }
    try {
      const result = await makeHttpRequest<WeatherInfo>(`http://www.weather.com.cn/data/cityinfo/${cityId}.html`, { dataType: 'json', });
      if (result.status === 200) {
        return result.data as WeatherInfo;
      }
    } catch (error) {
      throw new WeatherEmptyDataError();
    }

  }
}